package tag;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.Tag;

import toolkit.Counter;
import toolkit.Debug;
import toolkit.MultiCounter;

public abstract class AbstractTag implements Tag
{
	protected Tag tag = null;
	
	protected PageContext pageContext = null;
	
	private static MultiCounter tagIdentifiers = new MultiCounter();
	
	private String className = this.getClass().getName();

	protected long id = nextId();
	
	public AbstractTag()
	{
		Debug.log("(" + id + ") AbstractTag()");
	}
	
	private synchronized long nextId()
	{
		Debug.log(className + ".nextId()");
		
		Counter identifier = tagIdentifiers.get(className);
		identifier.increment();
		
		return identifier.count();
	}
	
	@Override
	public int doEndTag() throws JspException
	{
		Debug.log("(" + id + ") "+className+".doEndTag()");
		
		return SKIP_BODY;
	}
	
	@Override
	public void setParent(Tag tag)
	{
		Debug.log("(" + id + ") "+className+".setParent(" + tag + ")");
		
		this.tag = tag;
	}
	
	@Override
	public Tag getParent()
	{
		Debug.log("(" + id + ") "+className+".getParent()");
		
		return tag;
	}
	
	@Override
	public void setPageContext(PageContext pageContect)
	{
		Debug.log("(" + id + ") "+className+".setPageContext(" + pageContect + ")");
		
		pageContext = pageContect;
	}
	
	
	@Override
	public void release()
	{
		Debug.log("(" + id + ") "+className+".release()");
	}
	
	@Override
	protected void finalize() throws Throwable
	{
		Debug.log("(" + id + ") "+className+".finalize()");
	}
}
